#!/usr/bin/env python

import urllib2
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers

def submit(filepath, FROM_ADDR):
	
	try:
		filehandle = open(filepath, "rb")
	except IOError, e:
		print "Error: " + e.strerror
		print "Could not open " + filepath + "for reading"
		sys.exit(1)
	
	# This registers the poster module with urllib2
	register_openers()
	
	# Build the POST request
	url = "http://analysis.joebox.org/submit"
	params = { "email" : FROM_ADDR,
				"upfile" : filehandle,
				"service" : "1",
				"comments" : "",
				"upscript" : "",
				"submit" : "Analyse" }

	datagen, headers = multipart_encode(params)
	request = urllib2.Request(url, datagen, headers)
	#request.set_proxy("127.0.0.1:8080", "http")

	# Make the request
	try:
		response = urllib2.urlopen(request)
		result = response.read()
		print "Joebox: Check your email for results"
	except urllib2.URLError, e:
		print e.reason
		print "Joebox: Malware submission FAILED"
		
	filehandle.close()

if __name__ == "__main__":
	import sys
	
	submit(sys.argv[1], "malware@cryptocity.net")
